﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AlbumViewer : AlbumPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserDataHandler user = new UserDataHandler();
            if (user.CheckAccessRights(Album))
            {
                btnShowEdPan.Visible = true;
            }
            else
            {
                btnShowEdPan.Visible = false;
                btnHideEdPan.Visible = false;
                HideEditTools();
            }

            GenerateImgTable(int.Parse(ImagesPerPageDdlist.SelectedItem.Value));
        }
        LabelAlbumInfo.Text = Album.AlbumName;
        lblAuthor.Text = Album.Author;
        lblDate.Text = Album.CreationDate.Day + "." + Album.CreationDate.Month + "." + Album.CreationDate.Year + " " + Album.CreationDate.Hour + ":" + Album.CreationDate.Minute;
    }

    protected void ShowEditTools()
    {
        btnDelAlbum.Visible = true;
        btnDelImage.Visible = true;
        btnChanAlName.Visible = true;
        btnUploadImage.Visible = true;
        btnSendEmail.Visible = true;
    }
    protected void HideEditTools()
    {
        btnDelAlbum.Visible = false;
        btnDelImage.Visible = false;
        btnChanAlName.Visible = false;
        btnUploadImage.Visible = false;
        btnUploadImage.Visible = false;
        btnSendEmail.Visible = false;
    }
    protected void GenerateImgTable(int photosPerPage)
    {
        List<string> imgUrls = PhotoManager.GetUrlsOfAllPhotos(Server.MapPath(Album.FolderPath));
        if (imgUrls.Count != 0)
        {
            int startingPage = 1;
            if (photosPerPage != 0)
            {
                ImgTable.LoadParameters(imgUrls, startingPage, photosPerPage);
            }
            else
            {
                ImgTable.LoadParameters(imgUrls, startingPage, imgUrls.Count);
            }
            GeneratePages();
        }
        else
        {
            lblAlbumIsEmpty.Visible = true;
            HideImageControls();
        }
    }
    protected void HideImageControls()
    {
        btnBack.Visible = false;
        btnForward.Visible = false;
        ImagesPerPageDdlist.Visible = false;
        lblPhotosOnPage.Visible = false;
    }
    protected void GeneratePages()
    {
        int currentPage = ImgTable.CurrentPage;
        int range = 1;
        int addedPage;
        int highBorder = ImgTable.NumberOfPages;    
        mnuPagesList.Items.Clear();
        if (currentPage != 0)
        {
            mnuPagesList.Items.Add(new MenuItem("1"));
            addedPage = 1;
            for (int i = range; i >= 0; i--)
            {
                if (currentPage - i > 1 && currentPage-i>addedPage)
                {
                    mnuPagesList.Items.Add(new MenuItem("" + (currentPage - i)));
                    addedPage = currentPage - i;
                }
            }
            for (int i = 0; i <= range; i++)
            {
                if (currentPage + i < highBorder && currentPage + i > addedPage)
                {
                    mnuPagesList.Items.Add(new MenuItem("" + (currentPage + i)));
                    addedPage = currentPage + i;
                }
            }
            if (highBorder>addedPage)
                mnuPagesList.Items.Add(new MenuItem("" + highBorder));
            GetSelectedMenuPage();
        }
    }
    private void GetSelectedMenuPage()
    {
        foreach (MenuItem page in mnuPagesList.Items)
            if (page.Text == ImgTable.CurrentPage.ToString())
            {
                page.NavigateUrl = "#";
                page.Selected = true;
            }
    }

    protected void btnDelAlbum_Click(object sender, EventArgs e)
    {
        ImgTable.Clear();
        AlbumManager.DeleteAlbum(AlbumId);
        PhotoManager.DeleteAlbumPhotos(Server.MapPath(Album.FolderPath));
        Response.Redirect("~/Default.aspx");
    }

    protected void btnDelImage_Click(object sender, EventArgs e)
    {
          if (ImgTable.SelectedUrls != null)
          {
              if (ImgTable.SelectedUrls.Count == this.PhotoManager.GetPhotosNumber(Server.MapPath(Album.FolderPath)))
              {
                  AlbumManager.DeleteAlbum(AlbumId);
                  PhotoManager.DeleteAlbumPhotos(Server.MapPath(Album.FolderPath));
                  Response.Redirect("~/Default.aspx");
              }
              else
              {
                  bool result = this.PhotoManager.DeleteImages(ImgTable.SelectedUrls);

                  Response.Redirect(@"~/AlbumViewer.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + AlbumId.ToString());
              }
          }
    }

    public void ImageTable_Updated(object sender, EventArgs e)
    {
        PhotoManager = new PhotoManager(AlbumId);
        CurrentImageToView.ImageUrl = PhotoManager.GetFullUrlFromThumbNail(ImgTable.SelectedImageUrl);
        GeneratePages();
    }

    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        ImgTable.SelectPreviousImage();
    }
    protected void btnForward_Click(object sender, ImageClickEventArgs e)
    {
        ImgTable.SelectNextImage();
    }
    protected void ImagesPerPageDdlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ImagesPerPageDdlist.SelectedItem.Value != "0")
        {
            ImgTable.ImagesPerPage = int.Parse(ImagesPerPageDdlist.SelectedItem.Value);
            GeneratePages();
        }
        else
        {
            GenerateImgTable(0);
        }
    }
    protected void mnuPagesList_MenuItemClick(object sender, MenuEventArgs e)
    {
        ImgTable.CurrentPage = int.Parse(e.Item.Text);
    }
    protected void btnUploadImage_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/PhotoAdder.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + Server.UrlEncode(Album.ID.ToString()));
    }
    protected void btnNewAlName_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/AlbumRenamer.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + Server.UrlEncode(Album.ID.ToString()));
    }

    protected void btnShowEdPan_Click(object sender, EventArgs e)
    {
        ShowEditTools();
        btnShowEdPan.Visible = false;
        btnHideEdPan.Visible = true;
    }
    protected void btnHideEdPan_Click(object sender, EventArgs e)
    {
        HideEditTools();
        btnShowEdPan.Visible = true;
        btnHideEdPan.Visible = false;
    }

    protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/EmailSender.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + Server.UrlEncode(Album.ID.ToString()));
    }
}
